<template>
    <div class="dashboard-container">
        <!-- 统计卡片 -->
        <el-row :gutter="20" class="statistics-row">
            <el-col :span="6" v-for="(item, index) in statistics" :key="index">
                <div class="stat-card" :style="{ '--card-bg': item.color }">
                    <div class="stat-icon">
                        <el-icon>
                            <component :is="item.icon" />
                        </el-icon>
                    </div>
                    <div class="stat-content">
                        <div class="stat-value">{{ item.value }}</div>
                        <div class="stat-label">{{ item.label }}</div>
                    </div>
                    <div class="stat-trend">
                        <el-icon v-if="item.trend === 'up'" color="#67C23A">
                            <CaretTop />
                        </el-icon>
                        <el-icon v-else-if="item.trend === 'down'" color="#F56C6C">
                            <CaretBottom />
                        </el-icon>
                        <span :class="['trend-percentage', item.trend]">{{ item.trendPercentage }}</span>
                    </div>
                </div>
            </el-col>
        </el-row>

        <!-- 图表和活动区域 -->
        <el-row :gutter="20" class="chart-section">
            <el-col :span="16">
                <div class="chart-card">
                    <div class="chart-header">
                        <h3>访问统计</h3>
                        <el-radio-group v-model="chartTimeRange" size="small">
                            <el-radio-button value="week">本周</el-radio-button>
                            <el-radio-button value="month">本月</el-radio-button>
                            <el-radio-button value="year">全年</el-radio-button>
                        </el-radio-group>
                    </div>
                    <div ref="chartRef" class="chart-container"></div>
                </div>
            </el-col>
            <el-col :span="8">
                <div class="activity-card">
                    <div class="activity-header">
                        <h3>最近消息</h3>
                        <el-button type="primary">查看全部</el-button>
                    </div>
                    <div class="activity-list">
                        <div v-for="(message, index) in recentMessages" :key="index" class="message-item">
                            <el-avatar :size="48" :src="message.avatar" class="message-avatar" />
                            <div class="message-content" :class="{ unread: !message.isRead }">
                                <div class="message-header">
                                    <div class="message-sender">
                                        <el-icon v-if="message.type === 'announcement'" class="announcement-icon">
                                            <Bell />
                                        </el-icon>
                                        {{ message.sender }}
                                    </div>
                                    <el-tag v-if="!message.isRead" size="small" type="danger"
                                        class="unread-tag">未读</el-tag>
                                </div>
                                <div class="message-text">{{ message.content }}</div>
                                <div class="message-time">{{ message.time }}</div>
                            </div>
                        </div>
                    </div>
                </div>
            </el-col>
        </el-row>

        <!-- 快捷导航 -->
        <div class="shortcut-section">
            <div class="shortcut-header">
                <h3>快捷导航</h3>
            </div>
            <div class="shortcut-grid">
                <div v-for="menu in shortcuts" :key="menu.path" class="shortcut-item" @click="openMenu(menu)">
                    <div class="shortcut-icon">
                        <el-icon>
                            <component :is="menu.meta.icon" />
                        </el-icon>
                    </div>
                    <span class="shortcut-label">{{ menu.meta.title }}</span>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { useMenuStore } from '@/store/menu-store.js';
import * as echarts from 'echarts';
import { CaretTop, CaretBottom, Bell } from '@element-plus/icons-vue';
const recentMessages = ref([
    {
        sender: '张三',
        avatar: 'https://randomuser.me/api/portraits/men/1.jpg',
        content: '新版本的功能测试已完成，等待部署上线',
        time: '10分钟前',
        isRead: false,
        type: 'message',
    },
    {
        sender: '李四',
        avatar: 'https://randomuser.me/api/portraits/women/2.jpg',
        content: '客户反馈的Bug已修复，请查看最新代码',
        time: '30分钟前',
        isRead: true,
        type: 'message',
    },
    {
        sender: '王五',
        avatar: 'https://randomuser.me/api/portraits/men/3.jpg',
        content: '下午3点产品评审会议，请准时参加',
        time: '1小时前',
        isRead: false,
        type: 'message',
    },
    {
        sender: '赵六',
        avatar: 'https://randomuser.me/api/portraits/women/4.jpg',
        content: '已更新首页数据统计接口，请测试确认',
        time: '2小时前',
        isRead: true,
        type: 'message',
    },
    {
        sender: '系统公告',
        avatar: 'https://randomuser.me/api/portraits/lego/5.jpg',
        content: '服务器将于今晚凌晨2点进行例行维护',
        time: '1天前',
        isRead: false,
        type: 'announcement',
    },
    {
        sender: '系统公告',
        avatar: 'https://randomuser.me/api/portraits/lego/6.jpg',
        content: '系统已完成升级，新功能已上线',
        time: '2天前',
        isRead: true,
        type: 'announcement',
    },
]);
const systemStore = useMenuStore();
const shortcuts = ref([]);
const $router = useRouter();
const chartRef = ref(null);
const chartTimeRange = ref('week');
let chart = null;
// 统计数据
const statistics = ref([
    {
        label: '总用户数',
        value: '1,234',
        icon: 'User',
        color: 'linear-gradient(135deg, #4a90e2, #6a11cb)',
        trend: 'up',
        trendPercentage: '+12.5%',
    },
    {
        label: '今日访问',
        value: '423',
        icon: 'View',
        color: 'linear-gradient(135deg, #11998e, #38ef7d)',
        trend: 'up',
        trendPercentage: '+8.2%',
    },
    {
        label: '未读消息',
        value: '12',
        icon: 'Message',
        color: 'linear-gradient(135deg, #ff6a88, #ff6a88)',
        trend: 'down',
        trendPercentage: '-3.1%',
    },
    {
        label: '待办事项',
        value: '5',
        icon: 'List',
        color: 'linear-gradient(135deg, #f7b733, #fc4a1a)',
        trend: 'up',
        trendPercentage: '+5.7%',
    },
]);
const recentActivities = ref([
    {
        id: 1,
        avatar: 'https://randomuser.me/api/portraits/men/1.jpg',
        title: '用户 张三 创建了新项目',
        time: '2小时前',
        type: 'project',
        icon: 'Document',
    },
    {
        id: 2,
        avatar: 'https://randomuser.me/api/portraits/women/2.jpg',
        title: '李四 更新了系统设置',
        time: '5小时前',
        type: 'settings',
        icon: 'Setting',
    },
    {
        id: 3,
        avatar: 'https://randomuser.me/api/portraits/men/3.jpg',
        title: '王五 提交了代码变更',
        time: '今天',
        type: 'code',
        icon: 'Connection',
    },
    {
        id: 4,
        avatar: 'https://randomuser.me/api/portraits/women/4.jpg',
        title: '赵六 完成了性能优化',
        time: '昨天',
        type: 'performance',
        icon: 'DataLine',
    },
]);
const chartData = {
    week: {
        xAxis: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
        data: [820, 932, 901, 934, 1290, 1330, 1320],
    },
    month: {
        xAxis: ['1日', '5日', '10日', '15日', '20日', '25日', '30日'],
        data: [2200, 2900, 2800, 3500, 3200, 3100, 3300],
    },
    year: {
        xAxis: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'],
        data: [8200, 9320, 9010, 9340, 12900, 13300, 13200, 12800, 13800, 14200, 14800, 15500],
    },
};
onMounted(() => {
    treeToArray(systemStore.menuList, shortcuts.value);
    initChart();
});
// 树形菜单处理成数组
function treeToArray(tree, arr) {
    tree.forEach((item) => {
        if (item.meta?.isShortcut) {
            arr.push(item);
        }
        if (item.children) {
            treeToArray(item.children, arr);
        }
    });
}
// 快捷菜单跳转
function openMenu(menu) {
    $router.push(menu.path);
}
// 监听时间范围变化
watch(chartTimeRange, (newValue) => {
    updateChart(newValue);
});
// 初始化图表
function initChart() {
    if (!chartRef.value) return;

    chart = echarts.init(chartRef.value);
    updateChart(chartTimeRange.value);
}
// 更新图表数据
function updateChart(timeRange) {
    const data = chartData[timeRange];
    const option = {
        tooltip: {
            trigger: 'axis',
            backgroundColor: 'rgba(255, 255, 255, 0.9)',
            borderColor: 'rgba(0, 0, 0, 0.1)',
            borderWidth: 1,
            textStyle: {
                color: '#333',
            },
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true,
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: data.xAxis,
            axisLine: {
                lineStyle: {
                    color: 'rgba(0, 0, 0, 0.1)',
                },
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.6)',
            },
        },
        yAxis: {
            type: 'value',
            axisLine: {
                show: false,
            },
            splitLine: {
                lineStyle: {
                    color: 'rgba(0, 0, 0, 0.05)',
                },
            },
            axisLabel: {
                color: 'rgba(0, 0, 0, 0.6)',
            },
        },
        series: [
            {
                name: '访问量',
                type: 'line',
                smooth: true,
                data: data.data,
                areaStyle: {
                    opacity: 0.1,
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 0,
                        y2: 1,
                        colorStops: [
                            {
                                offset: 0,
                                color: 'rgba(74, 144, 226, 0.3)', // 渐变色
                            },
                            {
                                offset: 1,
                                color: 'rgba(74, 144, 226, 0.1)', // 渐变色
                            },
                        ],
                    },
                },
                lineStyle: {
                    width: 3,
                    color: {
                        type: 'linear',
                        x: 0,
                        y: 0,
                        x2: 1,
                        y2: 0,
                        colorStops: [
                            {
                                offset: 0,
                                color: '#4a90e2', // 渐变色
                            },
                            {
                                offset: 1,
                                color: '#6a11cb', // 渐变色
                            },
                        ],
                    },
                },
            },
        ],
    };

    chart.setOption(option);
}
// 监听图表容器大小变化
window.addEventListener('resize', () => {
    chart?.resize();
});
</script>

<style lang="scss" scoped>
.dashboard-container {
    background: linear-gradient(135deg, #f5f7fa 0%, #e6eaf0 100%);
    padding: 20px;
    min-height: 100vh;
    animation: fadeIn 0.8s ease-out;
    overflow: hidden;

    @keyframes fadeIn {
        from {
            opacity: 0;
            transform: translateY(20px);
        }

        to {
            opacity: 1;
            transform: translateY(0);
        }
    }

    // 统计卡片样式
    .statistics-row {
        margin-bottom: 20px;
        animation: slideIn 0.6s ease-out;

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateX(-20px);
            }

            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        .stat-card {
            display: flex;
            align-items: center;
            background: var(--card-bg);
            color: white;
            border-radius: 16px;
            padding: 24px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            position: relative;
            overflow: hidden;
            backdrop-filter: blur(10px);

            &::before {
                content: '';
                position: absolute;
                top: 0;
                left: 0;
                right: 0;
                bottom: 0;
                background: linear-gradient(45deg, rgba(255, 255, 255, 0.1) 0%, rgba(255, 255, 255, 0) 100%);
                z-index: 1;
            }

            &:hover {
                transform: translateY(-8px) scale(1.02);
                box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);

                .stat-icon {
                    transform: scale(1.1) rotate(10deg);
                }
            }

            .stat-icon {
                display: flex;
                align-items: center;
                justify-content: center;
                width: 64px;
                height: 64px;
                border-radius: 50%;
                background: rgba(255, 255, 255, 0.2);
                margin-right: 24px;
                transition: transform 0.4s ease;

                .el-icon {
                    font-size: 28px;
                }
            }

            .stat-content {
                flex-grow: 1;
                position: relative;
                z-index: 2;

                .stat-value {
                    font-size: 28px;
                    font-weight: 700;
                    margin-bottom: 8px;
                    letter-spacing: 0.5px;
                }

                .stat-label {
                    font-size: 15px;
                    opacity: 0.9;
                    letter-spacing: 0.3px;
                }
            }

            .stat-trend {
                display: flex;
                align-items: center;
                position: relative;
                z-index: 2;

                .trend-percentage {
                    margin-left: 8px;
                    font-size: 14px;
                    font-weight: 600;

                    &.up {
                        color: #67c23a;
                        text-shadow: 0 0 10px rgba(103, 194, 58, 0.4);
                    }

                    &.down {
                        color: #f56c6c;
                        text-shadow: 0 0 10px rgba(245, 108, 108, 0.4);
                    }
                }
            }
        }
    }

    // 图表区域样式
    .chart-section {
        margin-bottom: 20px;
        animation: slideUp 0.8s ease-out 0.2s both;

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(40px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .chart-card,
        .activity-card {
            background: rgba(255, 255, 255, 0.95);
            border-radius: 16px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            padding: 24px;
            height: 100%;
            max-height: 500px;
            backdrop-filter: blur(10px);
            transition: all 0.4s ease;
            display: flex;
            flex-direction: column;

            &:hover {
                transform: translateY(-5px);
                box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
            }

            .activity-list {
                flex: 1;
                overflow-y: auto;
                padding-right: 8px;

                &::-webkit-scrollbar {
                    width: 6px;
                }

                &::-webkit-scrollbar-thumb {
                    background-color: rgba(0, 0, 0, 0.1);
                    border-radius: 3px;
                }

                &::-webkit-scrollbar-track {
                    background-color: transparent;
                }
            }
        }

        .chart-header,
        .activity-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 24px;

            h3 {
                margin: 0;
                font-size: 20px;
                color: #2c3e50;
                font-weight: 600;
                letter-spacing: 0.5px;
            }
        }

        .chart-container {
            height: 350px;
        }

        .activity-list {
            .activity-item {
                display: flex;
                align-items: center;
                padding: 16px 0;
                border-bottom: 1px solid rgba(0, 0, 0, 0.05);
                transition: all 0.3s ease;

                &:hover {
                    transform: translateX(5px);
                    background: rgba(0, 0, 0, 0.02);
                }

                &:last-child {
                    border-bottom: none;
                }

                .activity-avatar {
                    margin-right: 16px;
                    transition: transform 0.3s ease;

                    &:hover {
                        transform: scale(1.1);
                    }
                }

                .activity-content {
                    flex-grow: 1;

                    .activity-title {
                        font-size: 15px;
                        color: #2c3e50;
                        margin-bottom: 6px;
                        font-weight: 500;
                    }

                    .activity-time {
                        font-size: 13px;
                        color: #6c757d;
                    }

                    .activity-type {
                        font-size: 13px;
                        color: #6c757d;
                        margin-top: 6px;
                        display: flex;
                        align-items: center;

                        .el-icon {
                            margin-right: 6px;
                            color: #4a90e2;
                        }
                    }
                }
            }
        }
    }

    // 快捷导航样式
    .shortcut-section {
        background: rgba(255, 255, 255, 0.95);
        border-radius: 16px;
        box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
        padding: 24px;
        animation: fadeInUp 0.8s ease-out 0.4s both;
        backdrop-filter: blur(10px);

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(40px);
            }

            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .shortcut-header {
            margin-bottom: 24px;

            h3 {
                margin: 0;
                font-size: 20px;
                color: #2c3e50;
                font-weight: 600;
                letter-spacing: 0.5px;
            }
        }

        .shortcut-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
            gap: 20px;

            .shortcut-item {
                display: flex;
                flex-direction: column;
                align-items: center;
                justify-content: center;
                padding: 20px;
                background: linear-gradient(135deg, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0.4) 100%);
                border-radius: 14px;
                cursor: pointer;
                transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
                border: 1px solid rgba(255, 255, 255, 0.2);

                &:hover {
                    transform: translateY(-5px) scale(1.02);
                    background: linear-gradient(135deg, rgba(74, 144, 226, 0.1) 0%, rgba(74, 144, 226, 0.05) 100%);
                    border-color: rgba(74, 144, 226, 0.3);

                    .shortcut-icon {
                        transform: scale(1.1) rotate(10deg);
                        color: #4a90e2;
                    }

                    .shortcut-label {
                        color: #4a90e2;
                    }
                }

                .shortcut-icon {
                    font-size: 24px;
                    margin-bottom: 12px;
                    color: #2c3e50;
                    transition: all 0.4s ease;
                }

                .shortcut-label {
                    font-size: 14px;
                    color: #2c3e50;
                    text-align: center;
                    transition: color 0.4s ease;
                }
            }
        }
    }
}

.message-item {
    display: flex;
    align-items: flex-start;
    padding: 12px;
    border-bottom: 1px solid var(--el-border-color-lighter);
    transition: background-color 0.3s;

    &:hover {
        background-color: var(--el-fill-color-light);
    }

    .message-avatar {
        margin-right: 12px;
    }

    .message-content {
        flex: 1;
        padding: 8px;
        border-radius: 4px;
        transition: background-color 0.3s ease;

        &.unread {
            background-color: var(--el-color-primary-light-9);
        }

        .message-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 4px;
        }

        .message-sender {
            font-weight: bold;
            color: var(--el-text-color-primary);
            display: flex;
            align-items: center;
            gap: 4px;

            .announcement-icon {
                color: var(--el-color-warning);
            }
        }

        .unread-tag {
            font-size: 12px;
            padding: 0 6px;
            height: 20px;
            line-height: 18px;
        }

        .message-text {
            color: var(--el-text-color-regular);
            margin-bottom: 4px;
            line-height: 1.4;
        }

        .message-time {
            font-size: 12px;
            color: var(--el-text-color-secondary);
        }
    }
}
</style>
@/store/menu@/store/menu.js@/store/menu-store.js@/store/menu-store.js